8319. Simple calculator

 

A simple calculator accepts the following types of strings as input:

·        num + num

·        num - num

·        num * num

·        num / num

where num is a positive integer between 1 and 10000 inclusive.

Find the value produced by the given expression. Here +, -, *, and / denote addition, subtraction, multiplication and division respectively. All operations are performed on integers, so “5 / 3” is 1.

 

Input. One string that a simple calculator accepts.

 

Output. Print the result of the given expression.

 

Sample input 1

Sample output 1

3 * 12

36

 

 

Sample input 2

Sample output 2

16 + 45

61

 

 

SOLUTION

strings

 

Algorithm analysis

The given example is read as a sequence of three tokens: number symbol number. Calculate the answer depending on the arithmetic operation.

 

Algorithm implementation

Read the input data.

 

scanf("%d %c %d",&a,&c,&b);

 

Compute the answer depending on the arithmetic operation.

 

if (c == '+') res = a + b;

if (c == '-') res = a - b;

if (c == '*') res = a * b;

if (c == '/') res = a / b;

 

Print the answer.

 

printf("%d\n",res);

 

Algorithm implementation – sscanf / sprintf

 

#include <iostream>

#include <string>

using namespace std;

 

char s[100];

int a, b, res;

char c;

 

int main(void)

{

  gets(s);

  sscanf(s, "%d %c %d", &a, &c, &b);

  if (c == '+') res = a + b;

  if (c == '-') res = a - b;

  if (c == '*') res = a * b;

  if (c == '/') res = a / b;

  sprintf(s, "%d", res);

  puts(s);

  return 0;

}

 

Algorithm implementation – STL

 

#include <iostream>

#include <string>

using namespace std;

 

int a, b, res;

char c;

 

int main(void)

{

  cin >> a >> c >> b;

  if (c == '+') res = a + b;

  if (c == '-') res = a - b;

  if (c == '*') res = a * b;

  if (c == '/') res = a / b;

  cout << res << endl;

  return 0;

}

 

Algorithm implementation – switch

 

#include <stdio.h>

 

int a, b, res;

char c;

 

int main(void)

{

  scanf("%d %c %d", &a, &c, &b);

 

  switch (c)

  {

    case '+':

      res = a + b;

      break;

    case '-':

      res = a - b;

      break;

    case '*':

      res = a * b;

      break;

    case '/':

      res = a / b;

  }

 

  printf("%d\n", res);

  return 0;

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    String ch = con.next();

    int b = con.nextInt();

   

    int res = 0;

    if (ch.equals("+")) res = a + b;

    if (ch.equals("-")) res = a - b;

    if (ch.equals("*")) res = a * b;

    if (ch.equals("/")) res = a / b;

 

    System.out.println(res);

    con.close();

  }

}

 

Java implementation – switch

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    String ch = con.next();

    int b = con.nextInt();

   

    int res = 0;

    switch (ch)

    {

      case "+": res = a + b; break;   

      case "-": res = a - b; break;

      case "*": res = a * b; break;

      case "/": res = a / b; break;

    }   

   

    System.out.println(res);

    con.close();

  }

}

 

Java implementation – class, version 1

 

import java.util.*;

 

class Number

{

  private int n;

  Number(int n)

  {

    this.n = n;

  }

 

  public int getValue()

  {

    return n;

  }

 

  public void Add(int a)

  {

    n += a;

  }

 

  public void Minus(int a)

  {

    n -= a;

  } 

 

  public void Multiply(int a)

  {

    n *= a;

  } 

 

  public void Divide(int a)

  {

    n /= a;

  } 

};

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    String ch = con.next();

    int b = con.nextInt();

   

    Number n = new Number(a);

    if (ch.equals("+")) n.Add(b);

    if (ch.equals("-")) n.Minus(b);

    if (ch.equals("*")) n.Multiply(b);

    if (ch.equals("/")) n.Divide(b);

   

    System.out.println(n.getValue());

    con.close();

  }

}

 

Java implementation – class, version 2

 

import java.util.*;

 

class Number

{

  private int n;

  Number()

  {

    this.n = 0;

  }

 

  Number(int n)

  {

    this.n = n;

  }

 

  public int getValue()

  {

    return n;

  }

 

  public Number Add(int a)

  {

    return new Number(n + a);

  }

 

  public Number Minus(int a)

  {

    return new Number(n - a);

  } 

 

  public Number Multiply(int a)

  {

    return new Number(n * a);

  } 

 

  public Number Divide(int a)

  {

    return new Number(n / a);

  } 

};

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    String ch = con.next();

    int b = con.nextInt();

   

    Number n = new Number(a);

    Number res = new Number();

    if (ch.equals("+")) res = n.Add(b);

    if (ch.equals("-")) res = n.Minus(b);

    if (ch.equals("*")) res = n.Multiply(b);

    if (ch.equals("/")) res = n.Divide(b);

   

    System.out.println(res.getValue());

    con.close();

  }

}

 

Python implementation

Read the input data.

 

a, sign, b = input().split()

a = int(a)

b = int(b)

 

Compute the answer depending on the arithmetic operation.

 

if sign == "+":

  res = a + b

if sign == "-":

  res = a – b

if sign == "*":

  res = a * b

if sign == "/":

  res = a // b

 

Print the answer.

 

print(res)